﻿using System;
using System.Drawing;
using WallpaperSharing.AppLibrary.Extensions;
using WallpaperSharing.AppLibrary.Service.Interface;
using WallpaperSharing.Domain;

namespace WallpaperSharing.AppLibrary.Service
{
    public class WallpaperService : IWallpaperService
    {
        public void DownloadWallpaper(Wallpaper wallpaper, string path = null)
        {
            var size = new Size(wallpaper.Width, wallpaper.Height);
            DownloadWallpaper(wallpaper, path, size);
        }

        public void DownloadWallpaper(Wallpaper wallpaper, Size size)
        {
            DownloadWallpaper(wallpaper, null, size);
        }

        public void DownloadWallpaper(Wallpaper wallpaper, string path, Size size)
        {
            const string destinationFormat = "{0}\\{1}.{2}";
            Image image = wallpaper.File.ToImage();

            var resizedImage = image;
            // Resize image only if size is changed
            if (size.Width != wallpaper.Width || size.Height != wallpaper.Height)
            {
                resizedImage = image.Resize(size);
            }

            if (path == null)
            {
                // Default directory is user's desktop
                path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            }

            string destination = String.Format(destinationFormat, path, wallpaper.Name, wallpaper.Extension);
            resizedImage.Save(destination);
        }

    }
}
